﻿using Blm.RabbitMQ.MessageQueue;
using Blm.RabbitMQ.Models;
using Blm.Utils.Extensions;
using Meta.RabbitMQ.Generic;
using Microsoft.Extensions.Logging;
using RabbitMQ.Client;
using System;
using System.Threading.Tasks;
using VisionCloud.Domain.DtoModel.RabbitMq;
using VisionCloud.Service.ElasticSearch;

namespace VisionCloud.Web.Service.MQListener
{
    public class AddSurfaceLogBackgroundService : BlmRabbitMQStorageSubscriberBase<SurfaceLogDto>
    {
        private readonly ILogger<AddSurfaceLogBackgroundService> logger;
        private readonly SurfaceLogEsService logEsService;

        public AddSurfaceLogBackgroundService(IServiceProvider serviceProvider,
            ILogger<AddSurfaceLogBackgroundService> logger,
            SurfaceLogEsService logEsService) : base(serviceProvider)
        {
            this.logger = logger;
            this.logEsService = logEsService;
        }

        public override StorageWayEm StorageWay => StorageWayEm.None;

        public override ushort ThreadCount => 5;

        public override ClientOption ClientOption => new ClientOption("surfacelog.ex", "surfacelog.add.rk", ExchangeType.Topic, "surfacelog.add.queue", RabbitMQConnectPoolName.CloudServerHost);

        public override async Task InvokeAsync(SurfaceLogDto model)
        {
            try
            {
                if (model == null)
                    throw new ArgumentNullException("反序列化SurfaceLogDto失败");

                await logEsService.WriteLog(model, SurfaceLogEsService.GetIndex());
            }
            catch (Exception ex)
            {
                logger.LogError(6000, ex, $"AddSurfaceLogBackgroundService出错：{ex.Message}，{ex.StackTrace}，队列消息：{model?.ToJsonStr()}");
            }
        }
    }
}
